﻿using PHRClient.DoctorForms.PatientRegister;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PHRClient.DoctorForms
{
    public partial class RegisterPatient : Form
    {
        public static FamilyMedicalHistory familyMedicalHistory = null;
        public static OtherRiskFactors otherRiskFactors = null;
        public static PastMedicalHistory pastMedicalHistory = null;
        public static PresentMedicalHistory presentMedicalHistory = null;
        public static GeneralInformation generalInformation = null;
        public static Settings settings = null;

        public static int newPatientId = PatientLoader.getLastInsertId() + 1;

        public RegisterPatient()
        {
            InitializeComponent();
            familyMedicalHistory = new FamilyMedicalHistory();            
            otherRiskFactors = new OtherRiskFactors();
            pastMedicalHistory = new PastMedicalHistory();
            presentMedicalHistory = new PresentMedicalHistory();
            generalInformation = new GeneralInformation();
            settings = new Settings();

            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(generalInformation);
            generalInformation.Show();
            
        }

        private void generalInfo_Click(object sender, EventArgs e)
        {
            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(generalInformation);
            generalInformation.Show();
        }

        private void presentMH_Click(object sender, EventArgs e)
        {
            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(presentMedicalHistory);
            presentMedicalHistory.Show();
        }

        private void pastMH_Click(object sender, EventArgs e)
        {
            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(pastMedicalHistory);
            pastMedicalHistory.Show();
        }

        private void familyMH_Click(object sender, EventArgs e)
        {
            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(familyMedicalHistory);
            familyMedicalHistory.Show();
        }

        private void otherRF_Click(object sender, EventArgs e)
        {
            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(otherRiskFactors);
            otherRiskFactors.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RegisterContainer.Controls.Clear();
            RegisterContainer.Controls.Add(settings);
            settings.Show();
        }

        private void register_Click(object sender, EventArgs e)
        {
            try
            {
                Patient patient = generalInformation.getPatientInfo();

                if (patient != null)
                {
                    Patient patientSec = settings.getPatientSecurity();

                    if (patientSec != null)
                    {
                        patient.PatientId = newPatientId;
                        patient.UserName = patientSec.UserName;
                        patient.Password = patientSec.Password;

                        var patients = new PatientCollection();
                        patients.Add(patient);
                        patients.Save(PatientLoader.XMLFILE);

                        MessageBox.Show("Patient Successfully Registerd!");
                        this.Close();
                    }
                    else
                    {
                        RegisterContainer.Controls.Clear();
                        RegisterContainer.Controls.Add(settings);
                        settings.Show();
                    }
                }
                else
                {
                    MessageBox.Show("Please Fill The Required Fields!!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Patient Registeration Failed!!, Check Input Data!!");
                RegisterContainer.Controls.Clear();
                RegisterContainer.Controls.Add(generalInformation);
                generalInformation.Show();
            }
            
        }
    }
}
